if(user()==null){
	attr("_httpurl","login?goto=users/");
}
function index(tbl){
	
}
function login(tbl){
	if(!isEmpty(tbl.goto)){
		sattr("goto",tbl.goto);
	}
	var user=dao.fetch("users",Cnd.wrap("(username='"+tbl.username+"' or email='"+tbl.username+"' or phone='"+tbl.username+"') and `password`='"+tbl.password+"'"));
	if(user==null&&!isEmpty(tbl.user_id)){
		user=dao.fetch("users",Cnd.where("id","=",tbl.user_id))
	}
	if(user!=null&&user.size()>0){
		dao.update("users",Chain.make("login_time",new java.util.Date()),Cnd.where("id","=",user.id))
		sattr("user",user);
		addCookie("user",URLEncoder.encode(toJson(user),"UTF-8"));
		if(!isEmpty(sattr("goto"))){
			attr("_httpurl",sattr("goto"))
			sattr("goto",null)
			return null;
		}
		attr("_httpurl","")
	}else{
		if(!isEmpty(sattr("goto"))){
			attr("_httpurl","login?goto="+sattr("goto"))
			return null;
		}
		attr("_httpurl","/login?goto=login&err=1")
	}
	return user;
}


function ajax_update(tbl){
	var u=user();
	if(u.id==tbl.id){
	tbl.forEach(function(k,v){
		u.put(k,v)
	})
	sattr("user",u);
	u.put(".table","users")
	$update(u)
	}
	return 1;
}
function favorite(tbl){
	var cnd=Cnd.where("user_id","=",user().id)
	if(tbl.name!=undefined){
		cnd.and("tbl_name","=",tbl.name)
		attr("name",tbl.name)
	}
	var list=$list("favorite",cnd)
	return list;
}
function exam(tbl){
	let cnd=Cnd.where("user_id","=",user().id).and("status","=",1)
	let orders=$list("orders",cnd);
	let ids=new ArrayList();
	orders.forEach(function(x){
		if(x.status==1){
			ids.add(x.tbl_id)
		}
	})
	let courses=$list("course",Cnd.where("id","in",ids).and("is_exam","=",1))
	return courses;
}
function orders(tbl){
	if(tbl.action!=undefined){
		$del({table:"orders",id:tbl.id})
	}
	var cnd=Cnd.where("user_id","=",user().id)
	if(tbl.name!=undefined){
		cnd.and("tbl_name","=",tbl.name)
		attr("name",tbl.name)
	}
	var list=$list("orders",cnd)
	return list;
}
function index(tbl){
	var list=$list("orders",Cnd.where("user_id","=",user().id).and("status","=",1))
	list.forEach(function(o){
		if(o.tbl_name=="course"){
			o.put("table",$fetch(o.tbl_name,Cnd.where("id","=",o.tbl_id)))
		}else if(o.tbl_name=="combo"){
			o.put("table",$fetch(o.tbl_name,Cnd.where("id","=",o.tbl_id)))
		}
		
	})
	return list;
}
//
function course(tbl){
	var order=$fetch("orders",Cnd.where("user_id","=",user().id).and("status","=",1).and("tbl_name","=","course").and("tbl_id","=",tbl.id))
	if(order!=null){
		attr("order",order)
		var course=$fetch("course",Cnd.where("id","=",tbl.id))
		var chapters=$list("chapter",Cnd.where("pid","=",0).and("tbl_name","=","course").and("tbl_id","=",course.id));
		chapters.forEach(function(c){
			c.put("list",$list("chapter",Cnd.where("pid","=",c.id)))
		})
		course.put("chapters",chapters)
		return course;
	}else{
		return ">>:users/";//未购买
	}
}
function combo(tbl){
	
	return ">>:combo/"+tbl.id;//未购买
}
function coupon(tbl){
	return null;
}
function balance(tbl){
	return null;
}
function contact(tbl){
	
}
function update(tbl){
	$update(tbl)
	let u=$fetch("users",Cnd.where("id","=",tbl.id));
	sattr("user",u)
	return ">>:users/contact"
}